package org.linlinjava.litemall.admin.controller;

import javax.validation.Valid;

import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.admin.beans.dto.I18nDto;
import org.linlinjava.litemall.admin.service.AdminI18nService;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 管理i18n控制器
 *
 * @author yliyun
 * @description：TODO
 * @date 2022/09/14
 */
@RestController
@RequestMapping("/admin/i18n")
public class AdminI18nController {
    /**
     * i18n服务
     */
    @Autowired
    private AdminI18nService i18nService;

    /**
     * 列表
     *
     * @param key   关键
     * @param type  类型
     * @param page  页面
     * @param limit 限制
     * @param sort  排序
     * @param order 订单
     * @return {@link Object}
     */
    @LogAnno
    @GetMapping("/list")
    public Object list(String key,
                       String type,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order){
        return i18nService.list(key, type, page, limit, sort, order);
    }

    /**
     * 更新
     *
     * @param dto dto
     * @return {@link Object}
     */
    @LogAnno
    @PutMapping("/update")
    public Object update(@Valid @RequestBody I18nDto dto){
        return i18nService.update(dto);
    }

    /**
     * 创建
     *
     * @param dto dto
     * @return {@link Object}
     */
    @LogAnno
    @PostMapping("/create")
    public Object create(@Valid @RequestBody I18nDto dto){
        return i18nService.create(dto);
    }

    /**
     * 创建
     *
     * @param key 关键
     * @return {@link Object}
     */
    @LogAnno
    @DeleteMapping("/delete")
    public Object create(@RequestParam("key") String key){
        return i18nService.delete(key);
    }


    /**
     * 通过关键
     * 获取国际化值Map
     *
     * @param key 关键
     * @return {@link Object}
     */
    @GetMapping("/get")
    public Object getByKey(@RequestParam("key") String key){
        return i18nService.get(key);
    }
}
